import './assets/main.css'
import './assets/css/admin.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue' // 导入所有图标组件
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

import App from './App.vue'
import router from './router'
import { useUserStore } from './stores/user'
import tokenManager from './utils/tokenManager'
import sessionGuard from './utils/sessionGuard'

const app = createApp(App)

// 注册所有Element Plus图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
})

// 应用启动时初始化用户信息
const userStore = useUserStore()
userStore.initializeUser()

// 如果用户已登录，启动token管理和会话守卫
if (userStore.isLoggedIn) {
  tokenManager.onLogin()
  sessionGuard.start()
}

app.mount('#app')
